package cn.iocoder.yudao.module.game.service.realtimeUserList;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.game.controller.admin.realtimeUserList.vo.*;
import cn.iocoder.yudao.module.game.dal.dataobject.realtimeUserList.RealtimeUserListDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.game.dal.mysql.realtimeUserList.RealtimeUserListMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.game.enums.ErrorCodeConstants.*;

/**
 * 实时用户列 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class RealtimeUserListServiceImpl implements RealtimeUserListService {

    @Resource
    private RealtimeUserListMapper realtimeUserListMapper;

    @Override
    public Long createRealtimeUserList(RealtimeUserListSaveReqVO createReqVO) {
        // 插入
        RealtimeUserListDO realtimeUserList = BeanUtils.toBean(createReqVO, RealtimeUserListDO.class);
        realtimeUserListMapper.insert(realtimeUserList);
        // 返回
        return realtimeUserList.getId();
    }

    @Override
    public void updateRealtimeUserList(RealtimeUserListSaveReqVO updateReqVO) {
        // 校验存在
        validateRealtimeUserListExists(updateReqVO.getId());
        // 更新
        RealtimeUserListDO updateObj = BeanUtils.toBean(updateReqVO, RealtimeUserListDO.class);
        realtimeUserListMapper.updateById(updateObj);
    }

    @Override
    public void deleteRealtimeUserList(Long id) {
        // 校验存在
        validateRealtimeUserListExists(id);
        // 删除
        realtimeUserListMapper.deleteById(id);
    }

    private void validateRealtimeUserListExists(Long id) {
        if (realtimeUserListMapper.selectById(id) == null) {
            throw exception(REALTIME_USER_LIST_NOT_EXISTS);
        }
    }

    @Override
    public RealtimeUserListDO getRealtimeUserList(Long id) {
        return realtimeUserListMapper.selectById(id);
    }

    @Override
    public PageResult<RealtimeUserListDO> getRealtimeUserListPage(RealtimeUserListPageReqVO pageReqVO) {
        return realtimeUserListMapper.selectPage(pageReqVO);
    }

}